import siot
import cv2
import socket
import numpy as np
import time
from keras.models import load_model

model = load_model('./model/lenet5.h5')

server = socket.socket(proto=socket.IPPROTO_TCP)
server.bind(('0.0.0.0', 8080))
server.listen(5)
print("Server is running.")
while True:
    client, addr = server.accept()
    msg = client.recv(16384).decode().strip()
    msg = np.array(list(map(int, msg.split())))
    try:
        msg = msg.reshape((28, 28))
    except ValueError:
        time.sleep(0.1)
        client.send('-1'.encode())
        client.close()
    else:
        ret = model.predict(np.array([msg]))
        prd = np.argmax(ret, axis=-1)
        print(prd, ret[0][prd[0]])
        client.send(str(prd[0] if ret[0][prd[0]] > 0.9 else -1).encode())
        client.close()
    

